<template>
  <a-modal :title="title" style="top: 100px;" :visible="true" width="1300px" @cancel="handleCancel">
    <div class="tips">
    </div>
    <a-table class="product-table" :rowKey="row => row.id" :dataSource="list" bordered :pagination="false" size="small">

      <a-table-column title="序号" width="20px">
        <template slot-scope="text, record, index">
          <div>{{ index+1 }}</div>
        </template>
      </a-table-column>


      <a-table-column title="开始时间" width="100px">
        <template slot-scope="row">
          <span>{{row.start_at}}</span>
        </template>
      </a-table-column>
      <a-table-column title="结束时间" width="100px">
        <template slot-scope="row">
          <span>{{row.end_at}}</span>
        </template>
      </a-table-column>
      <a-table-column title="领主奖励设置" width="80px">
        <template slot-scope="row">
          <a href="javascript:;" @click="handleEditLordSkuList(row)">
            <span class="">设置领主奖励({{row.rankings}})</span>
          </a>
        </template>
      </a-table-column>
      <a-table-column title="奖励记录" width="80px">
        <template slot-scope="row">
          <a href="javascript:;" @click="handleEditLog(row)">
            <span class="">奖励记录</span>
          </a>
        </template>
      </a-table-column>
      
      <a-table-column title="操作" width="120px">
        <template slot-scope="row">
          <a href="javascript:;" @click="handleEdit(row)">编辑</a>
          <a-divider type="vertical" />
            <a href="javascript:;" @click="handleCopy2(row)">新增(新)</a>
          <a-divider type="vertical" />
            <a href="javascript:;" @click="handleCopy(row)">复制</a>
          <a-divider type="vertical" />
           <a href="javascript:;" @click="handleDelete(row)">删除</a>
        </template>
      </a-table-column>
    </a-table>
    <template slot="footer">
      <div class="btn-c">
        <a-button type="primary" class="add" @click="handleCreate">
          新增一个
        </a-button>
        <div style="flex-grow: 1;"></div>

        <a-button key="back" @click="handleCancel">关闭</a-button>
        <!-- <a-button key="submit" type="primary" @click="handleSave">
          保存
        </a-button> -->
      </div>
    </template>
    <EditLordSkuList :info="current" v-if="isEditLordSkuList" @refresh="initList" @close="isEditLordSkuList = false"></EditLordSkuList>
     <a-modal title="领取列表" style="top: 100px;" :visible="isEditLog" width="1200px"
           @cancel="isEditLog = false" :footer="null">
    <LordLog v-if="isEditLog" :participant="participant" @cancel="isEditLog = false" @success="initList"></LordLog>
    </a-modal>
  </a-modal>
</template>
<script>
  import EditLordSkuList from './EditLordSkuListPopup.vue'
  import LordLog from './LordLog.vue'
  import UploadImages from '@/components/UploadImages/UploadImages'
  import iSwitch from '@/components/ISwitch/ISwitch'
  import ISkuType from '@/components/ISkuType/ISkuType'
  export default {
    components: {
      EditLordSkuList,
      LordLog,
      UploadImages,
      iSwitch,
      ISkuType
    },
    props: {
      info: {
        type: Object,
        default () {
          return {}
        }
      },
      currentSkuLevel: {
        type: Number,
        default () {
          return 1
        }
      }
    },
    data() {
      return {
        isEditLog: false,
        participant: [],
        current: {},
        isEditLordSkuList: false,
        list: [],
        listWeight: '',
        frame: [
          {
            title: '开始时间',
            key: 'start_at',
            type: 'date',
            newline: true,
            required: true,
          },
          {
            title: '结束时间',
            key: 'end_at',
            type: 'date',
            newline: true,
            required: true,
          },
          {
            title: '领主标记SKU_ID',
            key: 'sku_id',
            type: 'number',
            suffix: '',
            tips: '',
            newline: true,
            required: true,
          },
          {
            title: '规则文字',
            key: 'rule',
            type: 'textarea',
            placeholder: '一行一个说明',
            newline: true,
            tips: '不填则使用默认。 一行一个说明，不需要加序号。'
          },
        ],
        frame2: [
          {
            title: '开奖时间',
            key: 'end_at',
            type: 'date',
            newline: true,
            required: true,
            tips: '新版逻辑开始时间=开奖时间-间隔时间',
          },
          {
            title: '间隔时间',
            key: 'interval_time',
            type: 'number',
            default: 24,
            newline: true,
            required: true,
            suffix: '单位(小时)',
          },
          {
            title: '次数',
            key: 'num',
            type: 'number',
            default: 7,
            newline: true,
            required: true,
            suffix: '每天连续',
          },
          {
            title: '领主标记SKU_ID',
            key: 'sku_id',
            type: 'number',
            suffix: '',
            tips: '',
            newline: true,
            required: true,
          },
          {
            title: '规则文字',
            key: 'rule',
            type: 'textarea',
            placeholder: '一行一个说明',
            newline: true,
            tips: '不填则使用默认。 一行一个说明，不需要加序号。'
          },
        ]
      }
    },
    computed: {
      title() {
        return '领主活动时间'
      }
    },
    watch: {
      uuid() {
        this.initList()
      },
    },
    created() {
      this.initList()
    },
    methods: {
      handleCopy2(row) {
        this.$iForm.show({
          title: '主要是复制奖励',
          frame: this.frame2,
          success: (data) => {
            this.$http.post(`/fudais/${this.info.uuid}/lordnewadd`, {
              id: row.id,
              attributes: {
                ...data
              }
            }).then(res => {
              this.initList()
              this.$iForm.hide()
            })
          },
        })
      },
      handleCopy(row) {
        this.$iForm.show({
          title: '复制领主活动时间,复制主要是复制奖励',
          frame: this.frame,
          form: row,
          success: (data) => {
            this.$http.post(`/fudais/lord/${row.uuid}/copy`, {
              type: 'update',
              attributes: {
                ...data
              }
            }).then(res => {
              this.initList()
              this.$iForm.hide()
            })
          },
        })
      },
      handleEditLog(row) {
        this.participant = row.ranking_log
        this.isEditLog = true
      },
      handleEditLordSkuList(row) {
        this.current = row
        this.isEditLordSkuList = true
      },
      statusChange(e, row) {
        this.$http.put(`/fudai/lord/${row.uuid}`, {
          type: 'update',
          attributes: {
             status: e
          }
        }).then(res => {
          this.initList()
          // this.$iForm.hide()
        })
      },
      initList() {
        this.$http.get(`/fudais/${this.info.uuid}/lord`, {
          params: {
            activity_id: this.info.id
          }
        }).then(res => {
          this.list = res.data.list
        })
      },
      handleCreate() {
        this.$iForm.show({
          title: '新增领主活动时间',
          frame: this.frame,
          success: (data) => {
            this.$http.post(`/fudais/${this.info.uuid}/lord`, {
              ...data,
            }).then(res => {
              this.initList()
              this.$iForm.hide()
            })
          },
        })
      },
      handleEdit(row) {
        this.$iForm.show({
          title: '编辑领主活动时间',
          frame: this.frame,
          form: row,
          success: (data) => {
            this.$http.put(`/fudai/lord/${row.uuid}`, {
              type: 'update',
              attributes: {
                ...data
              }
            }).then(res => {
              this.initList()
              this.$iForm.hide()
            })
          },
        })
      },
      handleCancel() {
        this.$emit('refresh')
        this.$emit('close')
      },
      handleSave() {
        this.$emit('refresh')
        this.$emit('close')
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {

            this.$http.delete(`/fudai/lord/${row.uuid}`).then(res => {
              this.$message.success('删除成功')
              this.initList()
            })

          }
        })


      }
    }
  }
</script>
<style lang="less" scoped>
  .thumb {
    width: 60px;
    // height: 60px;
    border-radius: 6px;
  }

  .btn-c {
    display: flex;
    // margin-top: 30px;
    padding: 0rpx 30rpx;

    .add {}

    .save {
      margin-right: 20px;
    }

    .cancel {}
  }

  .tips {
    color: gray;
    font-size: 80%;
    margin-bottom: 10px;
  }

  a-input {
    display: inline;
  }
</style>
